import {defineStore} from "pinia"
import {ref} from "vue"
import {MenuOption, CascaderOption} from "naive-ui"
import {funThrottleByTime, arrConvert2Tree} from 'st-common-core'
import {useTipStore, TipMessageTypeConst} from 'st-ui-vue3-navie'
import {MenuObjPropNameEnum} from "../enums/menu-obj-prop-name-enum.ts"
import {MenuType} from "../types/menu-type.ts"
import {getMenus} from '../apis/menu-api.ts'
import {genMenuOptions, genMenuCascadeOptions} from '../utils/menu-data-util.ts'

/**
 * 系统菜单管理中菜单相关信息状态管理仓库
 */
export const useMenuStore = defineStore('menuStore', () => {
    // 菜单信息数据映射表（菜单 ID 与菜单信息数据项之间的映射）
  const menuDataMap = ref<Record<number, MenuType>>({})
  const getMenuDataMap = (): Record<number, MenuType> => menuDataMap.value
  const setMenuDataMap = (data: Record<number, MenuType>) => menuDataMap.value = data

  // 菜单级联选择器选项
  const menuCascadeOptions = ref<Array<CascaderOption>>([])
  const getMenuCascadeOptions = () => menuCascadeOptions.value
  const setMenuCascadeOptions = (newMenuCascadeOptions: Array<CascaderOption>) => menuCascadeOptions.value = newMenuCascadeOptions

  // 菜单选项
  const menuOptions = ref<MenuOption[]>([])
  const getMenuOptions = () => menuOptions.value
  const setMenuOptions = (options: MenuOption[]) => menuOptions.value = options

  // 激活的菜单项
  const activeMenu = ref<number>(2)
  const getActiveMenu = () => activeMenu.value
  const setActiveMenu = (menu: number) => activeMenu.value = menu

  // 当前选中的菜单项
  const menuKeySelected = ref<number>(0)
  const getMenuKeySelected = () => menuKeySelected.value
  const setMenuKeySelected = (selected: number = getActiveMenu()) => menuKeySelected.value = selected

  // 当前选中菜单的路径
  const menuSelectedPath = ref<MenuType[]>([])
  const getMenuSelectedPath = () => menuSelectedPath.value
  const setMenuSelectedPath = (path: number[] = getMenuDataMap()[getMenuKeySelected()][MenuObjPropNameEnum.PATH]) => {
    menuSelectedPath.value = []
    for (const key of path) {
      menuSelectedPath.value.push(getMenuDataMap()[key])
    }
  }

  // 通过请求获取菜单信息数据的处理函数
  const getMenuDataByReqHandler = async () => {
    // 发送请求获取菜单信息数据
    const res = await getMenus({
      isTree: false
    })
    // 获取失败
    if (!res.status) useTipStore().message('获取菜单失败，请刷新页面重试...', TipMessageTypeConst.ERROR)
    // 保存菜单信息数据
    const result = arrConvert2Tree(res.data, MenuObjPropNameEnum.ID, MenuObjPropNameEnum.PARENT_ID, true)
    // 更新菜单信息数据映射表
    setMenuDataMap(result.map)
    // 更新菜单级联选择器选项
    setMenuCascadeOptions(genMenuCascadeOptions(result.tree))
    // 更新菜单选项
    setMenuOptions(genMenuOptions(result.tree))
  }
  // 节流处理后的通过请求获取菜单信息数据的处理函数
  const getMenuDataByReq: typeof getMenuDataByReqHandler = funThrottleByTime({
    fun: getMenuDataByReqHandler,
    interval: 1000,
  })

  return {
    getMenuDataMap,
    setMenuDataMap,
    getMenuCascadeOptions,
    setMenuCascadeOptions,
    getMenuOptions,
    setMenuOptions,
    getActiveMenu,
    setActiveMenu,
    getMenuSelectedPath,
    setMenuSelectedPath,
    getMenuKeySelected,
    setMenuKeySelected,
    getMenuDataByReq,
  }
})
